import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';

import '../../../provider/config.dart';
import '../../../utils/utils.dart';

class ReferenceListWidget extends StatefulWidget {
  Function callBack;
  Map select_reference;
  Function cancelBack;
  ReferenceListWidget({
    Key? key,
    required this.callBack,
    required this.cancelBack,
    this.select_reference: const {},
  }) : super(key: key);

  @override
  State<ReferenceListWidget> createState() => _ReferenceListWidgetState();
}

class _ReferenceListWidgetState extends State<ReferenceListWidget> {
  int referenceIndex = -1;

  Map select_reference = {};

  List get evaluate_price_range {
    return Provider.of<ConfigProvider>(Get.context!, listen: false)
            .get("settings")?['evaluate_price_range'] ??
        [];
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.select_reference.isNotEmpty) {
      setState(() {
        select_reference = widget.select_reference;
        int indexWhere = evaluate_price_range
            .indexWhere((element) => element == select_reference);
        referenceIndex = indexWhere;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: Get.width - 24.w,
      padding: EdgeInsets.symmetric(
        horizontal: 12.w,
        vertical: 14.w,
      ),
      decoration: BoxDecoration(
        color: RGBA(235, 240, 239, 1),
        borderRadius: BorderRadius.circular(4.w),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              Text(
                "参考价",
                style: TextStyle(
                  fontSize: 14.sp,
                  color: rgba(51, 51, 51, 1),
                ),
              ),
              SizedBox(
                width: 8.w,
              ),
              Expanded(
                child: Container(
                  alignment: Alignment.centerLeft,
                  height: 28.w,
                  decoration: BoxDecoration(
                    color: rgba(255, 255, 255, 1),
                    borderRadius: BorderRadius.circular(4.w),
                  ),
                  padding: EdgeInsets.symmetric(
                    horizontal: 10.w,
                  ),
                  child: Text(
                    "${"${getPriceWithList(
                      select_reference['price_range'] ?? [],
                      intl: true,
                    )}"}",
                    style: TextStyle(
                      color: rgba(51, 51, 51, 1),
                      fontSize: 14.sp,
                    ),
                  ),
                ),
              ),
              GestureDetector(
                child: Container(
                  margin: EdgeInsets.only(
                    left: 20.w,
                  ),
                  width: 56.w,
                  height: 28.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: rgba(240, 69, 41, 1),
                    borderRadius: BorderRadius.circular(4.w),
                  ),
                  child: Text(
                    "确定",
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
                onTap: () {
                  if (referenceIndex == -1) {
                    widget.cancelBack();
                  } else {
                    select_reference = evaluate_price_range[referenceIndex];
                    widget.callBack(select_reference);
                  }
                },
              ),
            ],
          ),
          Wrap(
            alignment: WrapAlignment.start,
            spacing: 0,
            runSpacing: 0,
            crossAxisAlignment: WrapCrossAlignment.start,
            children: List.generate(
              evaluate_price_range.length,
              (index) {
                return GestureDetector(
                  child: Container(
                    margin: EdgeInsets.only(
                      right: 20.w,
                      top: 10.w,
                    ),
                    child: Center(
                      child: Text(
                        "${evaluate_price_range[index]['name']}",
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: referenceIndex == index
                              ? rgba(240, 69, 41, 1)
                              : rgba(51, 51, 51, 1),
                        ),
                      ),
                    ),
                    width: 80.w,
                    height: 32.w,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4.w),
                        color: referenceIndex == index
                            ? rgba(255, 240, 237, 1)
                            : rgba(255, 255, 255, 1),
                        border: Border.all(
                          width: 1,
                          color: referenceIndex == index
                              ? rgba(240, 69, 41, 1)
                              : Colors.transparent,
                        )),
                  ),
                  onTap: () {
                    setState(() {
                      referenceIndex = index;
                      select_reference = evaluate_price_range[index];
                    });
                  },
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
